package com.selfstudy.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.selfstudy.common.core.domain.model.LoginUser;
import com.selfstudy.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.selfstudy.common.annotation.Log;
import com.selfstudy.common.core.controller.BaseController;
import com.selfstudy.common.core.domain.AjaxResult;
import com.selfstudy.common.enums.BusinessType;
import com.selfstudy.system.domain.SOrder;
import com.selfstudy.system.service.ISOrderService;
import com.selfstudy.common.utils.poi.ExcelUtil;
import com.selfstudy.common.core.page.TableDataInfo;

/**
 * 预约信息Controller
 * 
 * @author lgf
 * @date 2024-01-30
 */
@Api("预约信息")
@RestController
@RequestMapping("/system/order")
public class SOrderController extends BaseController
{
    @Autowired
    private ISOrderService sOrderService;

    /**
     * 查询预约信息列表
     */
    @ApiOperation("预约查询")
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SOrder sOrder)
    {
        startPage();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if(SecurityUtils.hasRole("teacher") || SecurityUtils.isAdmin(loginUser.getUserId())){
            //查询所有
            sOrder.setUsername(null);
        }else{
            sOrder.setUsername(loginUser.getUsername());
        }
        List<SOrder> list = sOrderService.selectSOrderList(sOrder);

        return getDataTable(list);
    }

    /**
     * 导出预约信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SOrder sOrder)
    {
        List<SOrder> list = sOrderService.selectSOrderList(sOrder);
        ExcelUtil<SOrder> util = new ExcelUtil<SOrder>(SOrder.class);
        util.exportExcel(response, list, "预约信息数据");
    }

    /**
     * 获取预约信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sOrderService.selectSOrderById(id));
    }

    /**
     * 新增预约信息
     */
    @ApiOperation("预约新增")
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SOrder sOrder)
    {
        return toAjax(sOrderService.insertSOrder(sOrder));
    }

    /**
     * 修改预约信息
     */
    @ApiOperation("预约修改")
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SOrder sOrder)
    {
        return toAjax(sOrderService.updateSOrder(sOrder));
    }

    /**
     * 删除预约信息
     */
    @ApiOperation("预约删除")
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sOrderService.deleteSOrderByIds(ids));
    }
}
